﻿$(function () {
    $("#ELGngaynhan").datepicker({ dateFormat: 'dd/mm/yy' });
    $("#ELG_ngaynhan").datepicker({ dateFormat: 'dd/mm/yy' });   
});

$(function () {   
    $("#addngaynhan").datepicker({ dateFormat: 'dd/mm/yy' });
});

$(function () {
    $("#addngaynhanbct").datepicker({ dateFormat: 'dd/mm/yy' });
});

function ClientDoSaveDataLegal(NumUnit) {
    var ArrayUnit = Array();
    for (var iIndex = 0; iIndex < NumUnit; iIndex++) {
        ArrayUnit[iIndex] = document.getElementById("txt" + iIndex).value;
    }
    alert(ArrayUnit[0]);
}

function ShowhidedivLegalData() {
    selectid = document.getElementById("selectMinistry").options[document.getElementById("selectMinistry").selectedIndex].value;
    value = Portal.SODC.IDIS.AddNewData.HtmlContent(selectid).value;
    if (value.indexOf("ERROR") != -1) {
        alert(Url.replace("ERROR:", ""));
        return;
    }
    document.getElementById("addnewlegaldatacontent").innerHTML = value;
    document.getElementById("divMinistry").style.display = 'none';
    document.getElementById("addnewlegaldatacontent").style.display = 'block';

    if (document.getElementById("addngaynhanbct") != null)
        $("#addngaynhanbct").datepicker({ dateFormat: 'dd/mm/yy' });
    if (document.getElementById("addngaynhan") != null)
        $("#addngaynhan").datepicker({ dateFormat: 'dd/mm/yy' });
}

function checkdate(date) {
    var validformat = /^\d{2}\/\d{2}\/\d{4}$/;
    var returnval = false;
    if (!validformat.test(date))
        alert("Nhập sai định dạng ngày tháng.");
    else {
        var dayfield = date.split("/")[0];
        var monthfield = date.split("/")[1];
        var yearfield = date.split("/")[2];
        var dayobj = new Date(yearfield, monthfield - 1, dayfield);
        if ((dayobj.getMonth() + 1 != monthfield) || (dayobj.getDate() != dayfield) || (dayobj.getFullYear() != yearfield))
            alert("Vui lòng nhập lại ngày tháng theo định dạng ngày/tháng/năm.");
        else
            returnval = true;
    }
    return returnval;
}

function ClentResetAllFields(selectid) {
    if (selectid == "1") {
        document.getElementById("ctnk_kg").value = "";
        document.getElementById("ctnk_l").value = "";
        document.getElementById("ctxk_kg").value = "";
        document.getElementById("ctxk_l").value = "";
        document.getElementById("cttntx_kg").value = "";
        document.getElementById("cttntx_l").value = "";
        document.getElementById("ctsldsd_kg").value = "";
        document.getElementById("ctsldsd_l").value = "";
        document.getElementById("addngaynhanbct").value = "";
        document.getElementById("addngaynhanbct").focus();
    }  
    else {

        document.getElementById("addngaynhan").value = "";       
        for (var j = 1; j < 5; j++) {
            document.getElementById("valuelqc" + j).value="";
            document.getElementById("valuerqc" + j).value = "";
            document.getElementById("valuelnk" + j).value = "";
            document.getElementById("valuernk" + j).value = "";
            document.getElementById("valuelxk" + j).value = "";
            document.getElementById("valuerxk" + j).value = "";
            document.getElementById("valuelvc" + j).value = "";
            document.getElementById("valuervc" + j).value = "";
            document.getElementById("valueldd" + j).value = "";
            document.getElementById("valuerdd" + j).value = "";
        }
        document.getElementById("addngaynhan").focus();
    }
}

function ClientSaveLegalData(selectid) {
    var value;
   if (selectid == "1") {
        ctnk_kg = document.getElementById("ctnk_kg").value;
        ctnk_l = document.getElementById("ctnk_l").value;
        ctxk_kg = document.getElementById("ctxk_kg").value;
        ctxk_l = document.getElementById("ctxk_l").value;
        cttntx_kg = document.getElementById("cttntx_kg").value;
        cttntx_l = document.getElementById("cttntx_l").value;
        ctsldsd_kg = document.getElementById("ctsldsd_kg").value;
        ctsldsd_l = document.getElementById("ctsldsd_l").value;        
        var date = document.getElementById("addngaynhanbct").value;
        checkngaynhan = checkdate(date);
        if (checkngaynhan == false) {
            document.getElementById("addngaynhanbct").select();
            return;
        }
        value = Portal.SODC.IDIS.AddNewData.ServerAddnewLegalDataForBCT(selectid, ctnk_kg, ctnk_l, ctxk_kg, ctxk_l, cttntx_kg, cttntx_l, ctsldsd_kg, ctsldsd_l, date).value;       
    }
    else {          
        var date = document.getElementById("addngaynhan").value;
        checkngaynhan = checkdate(date);
        if (checkngaynhan == false) {
            document.getElementById("addngaynhan").select();
            return;
        }
        var quacanh=document.getElementById("valuelqc1").value;
        quacanh += ";" + document.getElementById("valuerqc1").value;
        var nhapkhau = document.getElementById("valuelnk1").value;
        nhapkhau += ";" + document.getElementById("valuernk1").value;
        var xuatkhau = document.getElementById("valuelxk1").value;
        xuatkhau += ";" + document.getElementById("valuerxk1").value;
        var laytuvc= document.getElementById("valuelvc1").value;
        laytuvc += ";" + document.getElementById("valuervc1").value;
        var dasudung= document.getElementById("valueldd1").value;
        dasudung += ";" + document.getElementById("valuerdd1").value;
            for (var j = 2; j < 5; j++) {
                quacanh += ";" + document.getElementById("valuelqc" + j).value ;
                quacanh += ";" + document.getElementById("valuerqc" + j).value;
                nhapkhau += ";" + document.getElementById("valuelnk" + j).value;
                nhapkhau += ";" + document.getElementById("valuernk" + j).value;
                xuatkhau += ";" + document.getElementById("valuelxk" + j).value;
                xuatkhau += ";" + document.getElementById("valuerxk" + j).value;
                laytuvc += ";" + document.getElementById("valuelvc" + j).value;
                laytuvc += ";" + document.getElementById("valuervc" + j).value;
                dasudung += ";" + document.getElementById("valueldd" + j).value;
                dasudung += ";" + document.getElementById("valuerdd" +j ).value;  
            }
        value = Portal.SODC.IDIS.AddNewData.ServerAddnewLegalData(selectid, date, quacanh, xuatkhau,nhapkhau,laytuvc,dasudung).value;
    }
    if (value.indexOf("ERROR") != -1) {
        alert(value.replace("ERROR:", ""));
        return;
    }
    alert(value);
    ClentResetAllFields(selectid);
}

function ClientSaveLegalDataFromEdit(selectid,recordid) {
    var value;
    if (selectid == "1") {
        ctnk_kg = document.getElementById("ctnk_kg").value;
        ctnk_l = document.getElementById("ctnk_l").value;
        ctxk_kg = document.getElementById("ctxk_kg").value;
        ctxk_l = document.getElementById("ctxk_l").value;
        cttntx_kg = document.getElementById("cttntx_kg").value;
        cttntx_l = document.getElementById("cttntx_l").value;
        ctsldsd_kg = document.getElementById("ctsldsd_kg").value;
        ctsldsd_l = document.getElementById("ctsldsd_l").value;
        date = document.getElementById("ELGngaynhan").value;        
        checkngaynhan = checkdate(date);
        if (checkngaynhan == false) {
            document.getElementById("ELGngaynhan").select();
            return;
        }
        value = Portal.SODC.IDIS.EditLegalData.ServerSaveLegalDataForBCT(recordid,selectid, ctnk_kg, ctnk_l, ctxk_kg, ctxk_l, cttntx_kg, cttntx_l, ctsldsd_kg, ctsldsd_l, date).value;
    }
    else {       
        var date = document.getElementById("ELG_ngaynhan").value;   
        checkngaynhan = checkdate(date);
        if (checkngaynhan == false) {
            document.getElementById("ELG_ngaynhan").select();
            return;
        }
        var quacanh = document.getElementById("valuelqc1").value;
        quacanh += ";" + document.getElementById("valuerqc1").value;
        var nhapkhau = document.getElementById("valuelnk1").value;
        nhapkhau += ";" + document.getElementById("valuernk1").value;
        var xuatkhau = document.getElementById("valuelxk1").value;
        xuatkhau += ";" + document.getElementById("valuerxk1").value;
        var laytuvc = document.getElementById("valuelvc1").value;
        laytuvc += ";" + document.getElementById("valuervc1").value;
        var dasudung = document.getElementById("valueldd1").value;
        dasudung += ";" + document.getElementById("valuerdd1").value;
        for (var j = 2; j < 5; j++) {
            quacanh += ";" + document.getElementById("valuelqc" + j).value;
            quacanh += ";" + document.getElementById("valuerqc" + j).value;
            nhapkhau += ";" + document.getElementById("valuelnk" + j).value;
            nhapkhau += ";" + document.getElementById("valuernk" + j).value;
            xuatkhau += ";" + document.getElementById("valuelxk" + j).value;
            xuatkhau += ";" + document.getElementById("valuerxk" + j).value;
            laytuvc += ";" + document.getElementById("valuelvc" + j).value;
            laytuvc += ";" + document.getElementById("valuervc" + j).value;
            dasudung += ";" + document.getElementById("valueldd" + j).value;
            dasudung += ";" + document.getElementById("valuerdd" + j).value;
        }
        value = Portal.SODC.IDIS.EditLegalData.ServerSaveLegalData(recordid,selectid, date, quacanh, xuatkhau, nhapkhau, laytuvc, dasudung).value;
    }
    if (value.indexOf("ERROR") != -1) {
        alert(value.replace("ERROR:", ""));
        return;
    }
    alert(value);  
    window.hi  
}

function ClientChangeValue() {
    combovalue = document.getElementById("selectype").options[document.getElementById("selectype").selectedIndex].value;
    document.getElementById("divforvalue1").style.display = 'none';
    document.getElementById("divforvalue2").style.display = 'none';
    document.getElementById("divforvalue3").style.display = 'none';
    document.getElementById("divforvalue4").style.display = 'none';
    document.getElementById("divforvalue"+combovalue).style.display = 'block';
}
function ClientDeleteLegalData(recordid,tablename) {
    Msg = Portal.SODC.IDIS.ListLegalData.GetConfirmDeleteMsg().value;
    if (confirm(Msg) == false) return;
    RetValue = Portal.SODC.IDIS.ListLegalData.ServerSideDeleteLegalData(recordid,tablename).value;
    if (RetValue.indexOf("ERROR") != -1) {
        alert(RetValue.replace("ERROR:", ""));
        return;
    }
    alert(RetValue);
    document.getElementById("divListLegalDataContent").innerHTML = Portal.SODC.IDIS.ListLegalData.BCTContent().value;

}

function ShowClientViewAddForm() {
    Url = Portal.SODC.IDIS.ListLegalData.GetShowAddnewForm().value;
    if (Url.indexOf("ERROR") != -1) {
        alert(Url.replace("ERROR:", ""));
        return;
    }
    window.open(Url, "_blank");
    RealFilterFunc(belongsite, MaxItems, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript);
}

function ShowClientViewdetailsForm(recordid, tableid) {
    Url = Portal.SODC.IDIS.ListLegalData.GetShowDetailsForm(recordid, tableid).value;
    if (Url.indexOf("ERROR") != -1) {
        alert(Url.replace("ERROR:", ""));
        return;
    }
    window.open(Url, "_self");
    RealFilterFunc(belongsite, MaxItems, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript);
}